package com.zygh.lz.vo;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.util.StrUtil;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.awt.*;
import java.io.*;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author
 * @Package com.zygh.webapi.controller.utils
 * @date 2022-08-22 15:27
 * @Copyright © 河南中裕广恒科技股份有限公司
 */
public class MyUtil {

    /**
     * 两个时间只差
     * @param
     * @param     * @return 分钟
     */
    public static Integer getBetweenMinutes(String b) {

        String now = DateUtil.now();

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startDate;
        Date endDate;
        try {
            startDate = format.parse(now);
            endDate = format.parse(b);
            try {
                long ss = 0;
                ss = startDate.getTime() - endDate.getTime();
                return (int) (ss/(60*1000));
            } catch (Exception e) {
                e.printStackTrace();
                return 0;
            }
        } catch (Exception e1) {
            e1.printStackTrace();
            return 0;
        }
    }

    private static InputStream base2InputStream(String base64string) {
        ByteArrayInputStream stream = null;
        try {
            BASE64Decoder decoder = new BASE64Decoder();
            byte[] bytes1 = decoder.decodeBuffer(base64string);
            stream = new ByteArrayInputStream(bytes1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stream;
    }

    private static String outstream2base(ByteArrayOutputStream outputStream){
        byte[] bytes = outputStream.toByteArray();
        // 转BASE64编码字符串
        BASE64Encoder encoder = new BASE64Encoder();
        String base64String = encoder.encodeBuffer(bytes).trim();
        return base64String.replaceAll("\n", "").replaceAll("\r", "");
    }

    public static String compressionBase64(String base64Str,float scale) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        ImgUtil.scale(
                base2InputStream(base64Str),
                outputStream,
                scale//缩放比例
        );

        return outstream2base(outputStream);
    }

 /*   public static String compressionImg(String imgUrl)throws Exception{
        URL url = new URL(imgUrl);
        Image image = ImgUtil.getImage(url);
        Image newimage = ImgUtil.scale(
                image,
                0.4f//缩放比例
        );
        return ImgUtil.toBase64(newimage,ImgUtil.IMAGE_TYPE_JPG);
    }*/

    /**
     * 判断当前时间是否在时间段内
     * @param beginTimeStr 起始时间，格式应与format匹配
     * @param endTimeStr 结束时间，格式应与format匹配
     * @param format 时间格式，如HH:mm:ss
     * @return boolean
     */
    public static boolean belongCalendar(String beginTimeStr, String endTimeStr, String format) {

        SimpleDateFormat df = new SimpleDateFormat(format);
        Date nowTime = null;
        Date beginTime = null;
        Date endTime = null;
        try {
            nowTime = df.parse(df.format(new Date()));
            beginTime = df.parse(beginTimeStr);
            endTime = df.parse(endTimeStr);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }


    public static void downloadImg(String base64Str, String fileName, String realPath) throws Exception {

        if (!StrUtil.isEmpty(base64Str)) {
            OutputStream out = null;
            try {
                String base64 = base64Str.split(",")[0];
                byte[] bytes = new BASE64Decoder().decodeBuffer(base64);
                for (int i = 0; i < bytes.length; ++i) {
                    if (bytes[i] < 0) {
                        bytes[i] += 256;
                    }
                }

                File file = new File(realPath);
                if (!file.exists() || !file.isDirectory()) {
                    file.mkdirs();
                }
                out = new FileOutputStream(realPath + "/" + fileName);
                out.write(bytes);
                out.flush();
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                if (out != null) {
                    out.close();
                }
            }
        }
    }


}
